//
//  PYTabBarController.m
//  IHome
//
//  Created by zhujizheng on 2020/2/15.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYTabBarController.h"
#import "PYMsgController.h"
#import "PYCommunityController.h"
#import "PYMineController.h"
#import "WHDebugToolManager.h"

@interface PYTabBarController () <UITabBarControllerDelegate>
/// 消息
@property (nonatomic, strong) PYMsgController *msgController;
/// 我的
@property (nonatomic, strong) PYMineController *mineController;
/// 社区
@property (nonatomic, strong) PYCommunityController *communityController;
@end

@implementation PYTabBarController

#pragma mark - msgController
- (PYMsgController *)msgController
{
    if (!_msgController) {
        _msgController = [PYMsgController new];
        _msgController.tabBarItem.title = @"消息";
        _msgController.tabBarItem.image = [UIImage imageNamed:@"message"];
    }
    return _msgController;
}

#pragma mark - mineController
- (PYMineController *)mineController
{
    if (!_mineController) {
        _mineController = [PYMineController new];
        _mineController.tabBarItem.title = @"我";
        _mineController.tabBarItem.image = [UIImage imageNamed:@"mine"];
    }
    return _mineController;
}

- (PYCommunityController *)communityController
{
    if (!_communityController) {
        _communityController = [PYCommunityController new];
        _communityController.tabBarItem.title = @"社区";
        _communityController.tabBarItem.image = [UIImage imageNamed:@"community"];
    }
    return _communityController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewControllers = @[self.msgController, self.communityController, self.mineController];
    self.selectedIndex = 0;
    self.tabBar.tintColor = PY_APP_Main_Color;
    self.tabBar.barTintColor = [UIColor whiteColor];
    self.delegate = self;
    self.navigationItem.title = @"消息";
    /// 修复tabBar高度问题
    [self repairTabBarHeight];
    // 监听通知
    [self addNotifications];
    
    [WHDebugToolManager showWith:(DebugToolTypeFPS | DebugToolTypeCPU | DebugToolTypeMemory)];
}

- (void)addNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChangedNotification:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}

- (void)statusBarFrameChangedNotification:(NSNotification *)notify
{
    [self checkStatusBarHeightAndRefreshUI];
}

- (void)checkStatusBarHeightAndRefreshUI
{
    UIView *rootView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
    CGRect rect = [self.view convertRect:self.tabBar.frame toView:rootView];
    CGFloat offset = rect.origin.y + rect.size.height - rootView.frame.size.height;
    self.tabBar.top -= offset;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self checkStatusBarHeightAndRefreshUI];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
    
    if (tabBarController.selectedIndex == 0) {
        self.navigationItem.title = @"消息";
    } else if (tabBarController.selectedIndex == 1) {
        self.navigationItem.title = @"社区";
    } else if (tabBarController.selectedIndex == 2) {
        self.navigationItem.title = @"我";
    }
}

#pragma mark - 修复tabBar高度问题
- (void)repairTabBarHeight
{
    [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([object isKindOfClass:[UITabBar class]] && [keyPath isEqualToString:@"frame"]) {
        UITabBar *tabBar = (UITabBar *)object;
        CGRect oldFrame = [[change objectForKey:NSKeyValueChangeOldKey] CGRectValue];
        CGRect newFrame = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
        if (CGSizeEqualToSize(oldFrame.size, newFrame.size) == NO) {
            if (oldFrame.size.height > newFrame.size.height) {
                tabBar.frame = oldFrame;
            } else {
                tabBar.frame = newFrame;
            }
        }
    }
}

#pragma mark - dealloc
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"frame"];
}

#pragma mark - 屏幕旋转

- (BOOL)shouldAutorotate
{
    return [PYScreenRotateManager shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//    if ([PYScreenRotateManager supportedInterfaceOrientations] == UIInterfaceOrientationMaskPortrait) {
//        [UIApplication sharedApplication].statusBarOrientation = [PYScreenRotateManager statusBarOrientation];
//    }
    return [PYScreenRotateManager supportedInterfaceOrientations];
}
/// 实现这个方法，解决导航栏问题,present 
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    return [PYScreenRotateManager statusBarOrientation];
//}

@end
